package com.baomidou.springwind.excel;

import com.baomidou.springwind.entity.*;
import com.baomidou.springwind.service.ReceiveGoodsService;
//import com.sun.xml.internal.ws.api.pipe.FiberContextSwitchInterceptor;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * excel文件读取
 *
 * @author luox
 */
@Component
public class RecieveDataReader extends ExcelReader<ReceiveGoods> {

    @Autowired
    private ReceiveGoodsService receiveGoodsService;

    @Override
    protected String saveData(List<ReceiveGoods> dataList, Long batchId) {
        for (ReceiveGoods goods : dataList) {
            goods.setBatchId(batchId);
            goods.setCreateTime(new Date());
            goods.setOperatorId(getCurrentUserId().intValue());
        }
        receiveGoodsService.insertBatch(dataList, 100);
        return "";
    }

    @Override
    protected List<ReceiveGoods> verifyData(HSSFWorkbook workbook) {
        List<ReceiveGoods> receiveGoods = new ArrayList<ReceiveGoods>();
        HSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        ReceiveGoods goods;
        for (int i = 1; i <= lastRowNum; i++) {
            sheet.getRow(i);
            goods = new ReceiveGoods();
            goods.setCode(getStringCellValue(sheet.getRow(i), 2));
            goods.setName(getStringCellValue(sheet.getRow(i), 3));
            goods.setArrivalTime(getDateCellValue(sheet.getRow(i), 4));
            goods.setAmount(getIntCellValue(sheet.getRow(i), 5));
            receiveGoods.add(goods);
        }
        return receiveGoods;
    }

    @Override
    protected List<ReceiveGoods> verifyData7(XSSFWorkbook workbook) {
        return null;
    }
}
